#ifndef BUFFER_H
#define BUFFER_H

#include <vector>
#include <cstring>
#include <string>
#include <iostream>

using std::string;

class StringPiece;

class Buffer {
public:
    static const size_t kCheapPrepend = 8;
    static const size_t kInitialSize = 10;
    Buffer(size_t initialSize = kInitialSize);
    
    int readFd(int fd, int* savedErrno);
    size_t prependableBytes();
    size_t readableBytes();
    const char* peek();
    size_t retrive(size_t len);
    void append(const char* str, size_t len);
    void append(const StringPiece& str);

    string toString();

    void printContent() {
        std::cout << " size: " << buffer_.size();
        std::cout << "  readableBytes: " << readableBytes();
        std::cout << "  writealbeBytes: " << writeableBytes();
        std::cout << " content: " << peek();
        std::cout << std::endl;
    }

private:
    size_t writeableBytes();
    void makeSpace(size_t len);  // 保证有len个可写字节空间

    std::vector<char> buffer_;

    size_t readerIdx_;
    size_t writerIdx_;
};


#endif